﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class BaseModule : UserControl, IModule
    {
        protected bool IsActive
        {
            get
            {
                if (ModuleUtils.GetForegroundWindow() != this.ParentForm.Handle)
                {
                    return false;
                }
                if (this.Parent is TabPage)
                {
                    TabPage tabPage = (TabPage)this.Parent;
                    TabControl tabCtl = (TabControl)tabPage.Parent;
                    return tabCtl.SelectedTab == tabPage;
                }
                else if (this.Parent is PopupForm)//弹出
                {
                    return true;
                }
                return false;
            }
        }

        public BaseModule()
        {
            InitializeComponent();
            this.HandleDestroyed += new EventHandler(BaseModule_HandleDestroyed);

            OnInitialize();//初始化
        }

        public void CloseModule()
        {
            bool bCanClose = true;
            OnBeforeClose(ref bCanClose);
            if (bCanClose)
            {
                DoClose();
            }
        }

        #region virtual Methods

        protected virtual void OnInitialize()
        {
        }

        protected virtual void OnInit()
        {
        }

        protected virtual void OnLoadModule()
        {
        }

        protected virtual void OnNew()
        {
        }

        protected virtual void OnModify()
        {
        }

        protected virtual void OnDelete()
        {
        }

        protected virtual void OnImportExcel()
        {
        }

        protected virtual void OnDownTemplate()
        {
        }

        protected virtual void OnRefresh()
        {
            OnLoadModule();
        }

        protected virtual void OnBeforeSave(ref bool _canSave)
        {
        }

        protected virtual void OnSave()
        {
        }

        protected virtual void OnAfterSave()
        {
        }

        protected virtual void OnBeforeClose(ref bool _canClose)
        {
        }

        protected virtual void OnModuleDestroyed()
        {
        }

        #endregion

        private void tsBtnNew_Click(object sender, EventArgs e)
        {
            OnNew();
        }

        private void tsBtnModify_Click(object sender, EventArgs e)
        {
            OnModify();
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            OnDelete();
        }

        private void tsBtnImportExcel_Click(object sender, EventArgs e)
        {
            OnImportExcel();
        }

        private void tsBtnDownTemplate_Click(object sender, EventArgs e)
        {
            OnDownTemplate();
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            bool bCanSave = true;
            OnBeforeSave(ref bCanSave);
            if (bCanSave)
            {
                OnSave();
            }
            OnAfterSave();
        }

        private void tsBtnRefresh_Click(object sender, EventArgs e)
        {
            OnRefresh();
        }

        private void tsBtnClose_Click(object sender, EventArgs e)
        {
            CloseModule();
        }

        private void BaseModule_Load(object sender, EventArgs e)
        {
            OnInit();
            OnLoadModule();
        }

        private void BaseModule_HandleDestroyed(object sender, EventArgs e)
        {
            OnModuleDestroyed();
        }

        private void DoClose()
        {
            if (this.Parent is TabPage)
            {
                TabPage tabPage = (TabPage)this.Parent;
                TabControl tabCtl = (TabControl)tabPage.Parent;

                tabCtl.TabPages.Remove(tabPage);
                tabPage.Dispose();
            }
            else if (this.Parent is PopupForm)
            {
                (this.Parent as PopupForm).DialogResult = DialogResult.Cancel;
            }
        }
    }
}
